package com.umer.pinban.customer;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.common.bean.InfoResult;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityListRequest;
import com.common.ui.loadsir.callback.EmptyCallback;
import com.common.ui.loadsir.callback.ErrorCallback;
import com.common.ui.loadsir.callback.NoNetCallback;
import com.common.ui.recycleview.PullLoadMoreRecyclerView;
import com.common.ui.recycleview.listener.DataStateListener;
import com.common.ui.recycleview.listener.OptListener;
import com.common.utils.Constants;
import com.common.utils.GsonUtils;
import com.common.utils.SpUtil;
import com.lxj.xpopup.XPopup;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.adapter.customer.CustomerXiaoFeiRecordAdapter;
import com.umer.pinban.base.BasicFragment;
import com.umer.pinban.bean.customer.XiaoFeiRecordBean;
import com.umer.pinban.customer.dialog.DialogOne;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

public class XiaoFeiFragment extends BasicFragment implements OptListener, DataStateListener,
        PullLoadMoreRecyclerView.PullLoadMoreListener{


    private static int customerId;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.pullLoadMoreRecyclerView)
    PullLoadMoreRecyclerView pullLoadMoreRecyclerView;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    private UserInfo userInfo;
    private CustomerXiaoFeiRecordAdapter customerXiaoFeiRecordAdapter;
    private List<XiaoFeiRecordBean> xiaoFeiRecordList = new ArrayList<>();

    public static XiaoFeiFragment newInstance(int argument) {
        customerId = argument;
        return new XiaoFeiFragment();
    }

    @Override
    public View isSupportLoadSirEnabled() {
        return llContent;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }


    @Override
    public int getLayoutResId() {
        return R.layout.fragment_customer_xiao_fei;
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        initView();
    }

    @Override
    public void loadData() {
        customerXiaoFeiRecordList();
    }

    @Override
    public void onOptClick(View view, Object object) {
        int flag = (int) object;
        switch (view.getId()){
            case R.id.linear_credentials:
                SpUtil.putString(getActivity(),Constants.xiaohaorecord,"");
                XiaoFeiRecordBean xiaoFeiRecord = customerXiaoFeiRecordAdapter.getDataSource().get(flag);
                String xiaohaorecord = GsonUtils.getInstance().toJson(xiaoFeiRecord);
                SpUtil.putString(getActivity(),Constants.xiaohaorecord,xiaohaorecord);
                Intent intent = new Intent(getActivity(),LookXiaoFeiRecordActivity.class);
                startActivity(intent);
                break;

            case R.id.linear_lay:
            case R.id.linear_lay3:
                String title = "";
                String time = String.valueOf(customerXiaoFeiRecordAdapter.getDataSource().get(flag).getExpenseDate());
                String num = String.valueOf(customerXiaoFeiRecordAdapter.getDataSource().get(flag).getOrderId());
                String remark = String.valueOf(customerXiaoFeiRecordAdapter.getDataSource().get(flag).getRemark());
                if(remark ==null || remark.equals("null") || remark.equals("")){
                    remark = "无备注信息";
                }
                //1：购买；
                //2：消耗；
                //3：退款；
                //4：余额购买
                int orderType = customerXiaoFeiRecordAdapter.getDataSource().get(flag).getOrderType();
                if(orderType == 1){
                    title = "购买订单";
                }else if(orderType == 2){
                    title = "消耗订单";
                }else if(orderType == 3){
                    title = "退款订单";
                }else if(orderType == 4){
                    title = "购买订单";
                }
                DialogOne dialogOne = new DialogOne(getActivity());
                dialogOne.setTime(time);
                dialogOne.setNum(num);
                dialogOne.setTitle(title);
                dialogOne.setRemark(remark);
                new XPopup.Builder(getContext())
                        .asCustom(dialogOne)
                        .show();
                break;
            default:break;
        }
    }

    @Override
    public void onRefresh() {
        customerXiaoFeiRecordAdapter.reset();
        loadData();
    }

    @Override
    public void onLoadMore() {
        loadData();
    }

    @Override
    public void onDataState(int flag) {
        //1:没有网络 2:没有数据 3:获取数据失败 4:显示内容
        switch (flag) {
            case 1:
                if (loadService != null) {
                    loadService.showCallback(NoNetCallback.class);
                }
                break;
            case 2:
                if (loadService != null) {
                    loadService.showCallback(EmptyCallback.class);
                }
                break;
            case 3:
                if (loadService != null) {
                    loadService.showCallback(ErrorCallback.class);
                }
                break;
            case 4:
                if (loadService != null) {
                    loadService.showSuccess();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.customerXiaoFeiRecordList:
                if(swipeRefreshLayout!=null && swipeRefreshLayout.isRefreshing()){
                    swipeRefreshLayout.setRefreshing(false);
                }
                xiaoFeiRecordList = (List<XiaoFeiRecordBean>) t.getT();
                customerXiaoFeiRecordAdapter.addDataSource(xiaoFeiRecordList, t);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.customerXiaoFeiRecordList:
                if(swipeRefreshLayout!=null && swipeRefreshLayout.isRefreshing()){
                    swipeRefreshLayout.setRefreshing(false);
                }
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    private void customerXiaoFeiRecordList() {
        OkEntityListRequest<XiaoFeiRecordBean> request = new OkEntityListRequest<>(R.id.customerXiaoFeiRecordList,
                Constants.customerXiaoFeiRecordList, XiaoFeiRecordBean.class);
        request.addParams("customerId", customerId);
        request.addParams("shopId", userInfo.getShopId());
        request.addParams("page",customerXiaoFeiRecordAdapter.getPage());
        request.addParams("pageSize",10);
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }
    private void initView() {

        pullLoadMoreRecyclerView.init(recyclerView, swipeRefreshLayout);
        customerXiaoFeiRecordAdapter = new CustomerXiaoFeiRecordAdapter(getActivity(),
                R.layout.fragment_xiaofei_record_item,
                pullLoadMoreRecyclerView,
                this,this);
        pullLoadMoreRecyclerView.setAdapterTwo(customerXiaoFeiRecordAdapter);
        pullLoadMoreRecyclerView.setOnPullLoadMoreListener(this);
        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimary, R.color.colorPrimary);
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法

       /* //单击
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView())
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                    }
                });
        //长按
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView()).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
                return false;
            }
        });*/
    }
}
